package cn.chencaiju.day10;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

import org.junit.Test;

public class FileWallkerTest {
	/**
	 * 工具类遍历文件夹下的文件
	 * 查找特定文件
	 * @throws IOException
	 */
	public void OverDir() throws IOException {
		Path path = Paths.get("E:\\camp");
		Files.walkFileTree(path, new FileVisitor<Path>() {
			@Override
			public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
				return FileVisitResult.CONTINUE;
			}

			@Override
			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
				int index = file.toString().lastIndexOf(".");
				// 求子字符
				String suffix = file.toString().substring(index + 1);
				if (suffix.equals("java")) {
					System.out.println(file.getFileName());
				}
				return FileVisitResult.CONTINUE;
			}

			@Override
			public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
				return FileVisitResult.CONTINUE;
			}

			@Override
			public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
				return FileVisitResult.CONTINUE;
			}
		});
	}

	@Test public void testListDir() throws IOException {
		OverDir();
	}
}
